package com.xfhy.component;

import android.content.Context;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Interceptor;
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
import com.alibaba.android.arouter.facade.template.IInterceptor;
import com.xfhy.componentbase.ServiceFactory;

/**
 * @author : xfhy
 * Create time : 2020-03-01 14:06
 * Description : 登录状态过滤拦截器
 */
@Interceptor(priority = 8, name = "登录状态拦截器")
public class LoginInterceptor implements IInterceptor {

    private Context mContext;

    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {
        // onContinue 和 onInterrupt 至少需要调用其中一种，否则不会继续路由

        if (postcard.getPath().equals("/share/share")) {
            if (ServiceFactory.getInstance().getAccountService().isLogin()) {
                //处理完成,交还控制权
                callback.onContinue(postcard);
            } else {
                //中断路由流程
                callback.onInterrupt(new RuntimeException("请登录"));
            }
        } else {
            callback.onContinue(postcard);
        }

    }

    @Override
    public void init(Context context) {
        mContext = context;
    }
}
